<?php
class Customer_Form_Customer extends TBB_Form
{	
	public function init()
	{			
		//$this->addElementPrefixPath ( 'TBB_Validate', 'TBB/Validate/', 'validate' );
		$this->addPrefixPath('TBB_Form_Decorator', 'TBB/Form/Decorator', 'decorator');
		
		$this->addAttribs(array(
			'class' => 'span-7',
			'id' => 'customerForm'
		));

		////////////////////// create name element //////////////////////
		$name = $this->_createNameElement();
		
		////////////////////// create email element //////////////////////
		$email = $this->_createEmailElement();
		
		////////////////////// create SSN element //////////////////////////
		$ssn = $this->_createSSNElement();
		
		////////////////////// create mobile //////////////////////
		$mobile = $this->_createMobileElement();
		$this->addElement($mobile);
		
		////////////////////// create email login button //////////////////////
		$submit = $this->_createSubmitElement();		
		
		///////////////////// trip id hidden element ////////////////////		
		$id = $this->_createTripIdElement();
		
		///////////////////// seat count hidden element //////////////////
		$seatCountYouWant = $this->_createSeatCountYouWantElement();
		
		$this->addElements(array($name, $email, $ssn, $mobile, $submit, $id, $seatCountYouWant));
		$this->addDisplayGroup(array('name', 'email', 'ssn', 'mobile', 'submit'), 'displayGroup');
		
		$this->setDecorators($this->_formDecorators);
		$this->setDisplayGroupDecorators($this->_displayGroupDecorators);
	}
	
	private function _createSubmitElement()
	{
		$submit = $this->createElement('submit', 'submit')	
					   ->setLabel('Tiếp tục')
					   ->setDecorators($this->_submitDecorators)	
					   ->setAttribs(array(
							'class' => 'span-4 submitDecorator',
					   		'id' => 'submitOnCustomer'
						));
		return $submit;
	}
	
	private function _createNameElement()
	{
		$name = $this->createElement('text', 'name');
		$name->setLabel('Tên khách hàng: ');
		$name->setRequired(true);
		$name->addFilter('StripTags');
		$name->addErrorMessage('Làm ơn nhập tên khách hàng.');
		$name->setAttribs(array(
			'class' => 'column span-6 last'
		));		
		$name->getDecorator('Label')->setOption('class', 'column span-4 labelDecorator');
		$name->getDecorator('Errors')->setOption('class', 'span-6 errorDecorator');
		return $name;
	}
	
	private function _createEmailElement()
	{
		$email = $this->createElement('text', 'email');
		$email->setLabel('Email: ');
		$email->setRequired(true);
		$email->addFilters(array('StringTrim', 'StringToLower'));
		// create email NotEmpty validator
		$emailNotEmptyValidator = $this->_createNotEmptyValidator('Làm ơn nhập email');		
		// create email StringLength validator
		$emailStringLengthValidator = $this->_createStringLengthValidator(3, 128,
								 'Email quá ngắn. Email phải có ít nhất là %min% chữ.', 
								 'Email quá dài. Email chỉ được có nhiều nhất là %max% chữ.');
		// create mail EmailAddress validator
		$emailValidator = $this->_createEmailEmailAddressValidator('Email không hợp lệ.');
		$email->addValidators(array(
			array($emailNotEmptyValidator, true ), // true = break validation chain
			array($emailStringLengthValidator, true ),
			array($emailValidator, true)
		));
		//******** to-do thing in future, add UniqueEmail validator *******////////
		$email->setAttribs(array(
			'class' => 'column span-6 last',
		));
		//$email->getDecorator('ViewHelper')->setOption('class', 'column span-6');
		$email->getDecorator('Label')->setOption('class', 'column span-4 labelDecorator');
		$email->getDecorator('Errors')->setOption('class', 'span-6 errorDecorator');	
		return $email;
	}
	
	private function _createSSNElement()
	{
		$ssn = $this->createElement('text', 'ssn');
		$ssn->setLabel('CMND: ');
		$ssn->setRequired(true);
		$ssn->addFilters(array('StringTrim', 'StringToLower'));
		// create NotEmpty validator
		$ssnNotEmptyValidator = $this->_createNotEmptyValidator('Làm ơn nhập CMND');
		// create StringLength validator
		$ssnStringLengthValidator = $this->_createStringLengthValidator(9, 9, 
								'CMND quá ngắn. CMND phải có %min% chữ số.',
								'CMND quá dài. CMND phải có %min% chữ số.');
		$ssn->addValidators(array(
			array($ssnNotEmptyValidator, true),
			array($ssnStringLengthValidator, true),
		)); // array('UniqueEmail', false, array(Home_Model_User()))
		$ssn->setAttribs(array(
			'class' => 'column span-6 last',
		));		
		$ssn->getDecorator('Label')->setOption('class', 'column span-4 labelDecorator');
		$ssn->getDecorator('Errors')->setOption('class', 'span-6 errorDecorator');		
		return $ssn;	
	}

	private function _createMobileElement()
	{
		$mobile = $this->createElement('text', 'mobile');
		$mobile->setLabel('Số điện thoại: ');
		$mobile->setRequired(true);
		// create NotEmpty validator
		$mobileNotEmptyValidator = $this->_createNotEmptyValidator('Làm ơn nhập số điện thoại.');
		$mobileRegexValidator = $this->_createRegexValidator('/[0-9]{7,20}/', 'Số điện thoại không hợp lệ');		
		$mobile->addValidators(array(
			array($mobileNotEmptyValidator, true),
			array($mobileRegexValidator, true)
		));
		$mobile->setAttribs(array(
			'class' => 'column span-6 last'
		));
		$mobile->getDecorator('Label')->setOption('class', 'column span-4 labelDecorator');
		$mobile->getDecorator('Errors')->setOption('class', 'span-6 errorDecorator');
		return $mobile;
	}
	
	private function _createTripIdElement()
	{
		$id = $this->createElement ( 'hidden', 'trip' );
		$id->setDecorators($this->_hiddenDecorators);
		$id->setAttribs(array(
			'class' => 'span-4',
		));
		return $id;
	}
	
	private function _createSeatCountYouWantElement()
	{
		$seatCountYouWant = $this->createElement ( 'hidden', 'seatCountYouWant' );
		$seatCountYouWant->setDecorators($this->_hiddenDecorators);
		$seatCountYouWant->setAttribs(array(
			'class' => 'span-4',
		));
		return $seatCountYouWant;
	}
		
	public function addSeats($seats)
	{		
		if($seats == null) throw new Exception('seat is null');	
		$subForm = new Zend_Form_SubForm(array('class' => 'addSeatStyle'));	
		$subForm->setDecorators($this->_displayGroupDecorators);
		foreach($seats as $seatNumber => $isBooked) {			
			$subForm->addElement('text', $seatNumber . '', array('value' => $isBooked,
														  'decorators' => array('ViewHelper')));			
		}
		$this->addSubForm($subForm, 'seats');
	}
	
	public function addTempSeats($tempSeats)
	{
		if($tempSeats == null) throw new Exception('tempSeats is null');	
		$subForm = new Zend_Form_SubForm(array('class' => 'addSeatStyle'));	
		$subForm->setDecorators($this->_displayGroupDecorators);
		foreach($tempSeats as $index => $ticketID) {			
			$subForm->addElement('hidden', $index . '', array('value' => $ticketID,
														  'decorators' => array('ViewHelper')));			
		}
		$this->addSubForm($subForm, 'tempSeats');
	}
}